/**
 * Copyright 2016 IFlyTek. All rights reserved.
 */

package com.jianzh5.action.admin;

import com.jianzh5.action.BaseAction;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.entity.TNews;
import com.jianzh5.entity.enums.NewsTypeEnum;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.service.NewsServiceI;
import com.jianzh5.util.InfoCenterHome;
import com.jianzh5.util.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * <code>ArticleAction</code>
 * </p>
 * 新闻控制器
 * @author jianzhang11
 * @version 1.0 2016/12/28
 */
@Controller
@RequestMapping("/cms/news")
public class NewsAction extends BaseAction{
    @Autowired
    private NewsServiceI newsService;

    @RequestMapping(value = "/newsList" ,method = RequestMethod.GET)
    @RequiresPermissions("cms:news:view")
    public String newsList(){
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"cms/newsList";
    }

    @RequestMapping(value = "/newsList",method = RequestMethod.POST)
    @ResponseBody
    public DataTables newsList(DataPage dataPage ,HttpServletRequest request){
        String sindex = request.getParameter("order[0][column]");
        String order = request.getParameter("order[0][dir]");
        String sort = request.getParameter("columns["+sindex+"][data]");
        DataTables dt = newsService.dataTables(order,sort,dataPage);
        return dt;
    }

    @RequestMapping("/editNews")
    @RequiresPermissions("cms:news:edit")
    public String editNews(TNews news,Model model){
        model.addAttribute("newsType", NewsTypeEnum.newsTypeEnumMap);
        if(news.getId() != null && news.getId() != 0){
            //编辑，需加载游戏信息
            news = newsService.getNews(news.getId());
            model.addAttribute("news",news);
        }
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"cms/newsEdit";
    }

    @RequestMapping("/newsEdit")
    @ResponseBody
    public ResultJson newsEdit(TNews news, HttpServletRequest request){
        PAdmin pAdmin = UserUtils.getUser();
        ResultJson json = new ResultJson();
        try {
            newsService.updateNews(news,pAdmin);
            json.setSuccess(true);
            json.setMsg("新闻编辑成功...");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("新闻编辑失败",e);
            json.setMsg("新闻编辑失败,数据库异常...");
        }
        return json;
    }


    @ResponseBody
    @RequestMapping("/newsDel")
    @RequiresPermissions("cms:news:del")
    public ResultJson productDel(@RequestParam("id") int id){
        ResultJson json = new ResultJson();
        try{
            newsService.delNews(id);
            json.setSuccess(true);
            json.setMsg("新闻刪除成功...");
        }catch (Exception e) {
            logger.error("新闻刪除失败,数据库异常...",e);
            json.setMsg("新闻刪除失败,数据库异常...");
        }
        return json;
    }

}
